package com.haixiaoke.flat.house.controller;


import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.haixiaoke.auth.AuthCheck;
import com.haixiaoke.saas.common.constant.HouseConstants;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.house.domain.HousePicture;
import com.haixiaoke.saas.house.service.IHousePictureService;
import com.haixiaoke.saas.system.BaseController;
import com.haixiaoke.token.FrontTokenComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 房屋相册信息Controller
 *
 * @author xiaolong
 * @date 2024-10-22
 */
@RestController
@RequestMapping("/front/flat/house/picture")
public class FlatHousePictureController extends BaseController {
    @Autowired
    private IHousePictureService housePictureService;
    @Autowired
    private FrontTokenComponent frontTokenComponent;

    /**
     * 获取房屋相册信息详细信息
     */
    @GetMapping(value = "/{housePictureId}")
    @AuthCheck
    public AjaxResult getInfo(@PathVariable("housePictureId") Long housePictureId) {
        return success(housePictureService.selectHousePictureByHousePictureId(housePictureId));
    }


    /**
     * 新增日常检查
     */
    @PostMapping
    @AuthCheck
    public AjaxResult add(@RequestBody HousePicture housePicture) {
        if (StrUtil.isBlank(housePicture.getPictureSet())) {
            throw new ServiceException("请上传照片！");
        }
        if (housePicture.getPictureSet().split(",").length > 6) {
            throw new ServiceException("照片不能超过6张！");
        }
        housePicture.setHousePictureType(HouseConstants.HOUSE_PICTURE_DAILY);
        housePicture.setUploadUser(frontTokenComponent.getLoginUser().getUser().getUserName());
        housePicture.setUploadUserId(frontTokenComponent.getLoginUser().getUser().getUserId());
        housePicture.setPictureNumber(housePicture.getPictureSet().split(",").length);
        // 生成分组编号
        String packetIdentification = RandomUtil.randomStringWithoutStr(10, "0123456789");
        housePicture.setPacketIdentification(packetIdentification);
        return toAjax(housePictureService.insertHousePicture(housePicture));
    }

    /**
     * 根据用户id获取房屋相册列表
     */
    @GetMapping(value = "/appletList")
    @AuthCheck
    public TableDataInfo appletList(HousePicture housePicture) {
        startPage();
        housePicture.setUploadUserId(frontTokenComponent.getLoginUser().getUser().getUserId());
        housePicture.setHousePictureType(HouseConstants.HOUSE_PICTURE_DAILY);
        return getDataTable(housePictureService.selectHousePictureList(housePicture));
    }

}
